// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/exec/unordered_stream_reader.h>
#include <pollux/vector/vector_fuzzer.h>
#include <pollux/testing/vector/vector_test_base.h>

using namespace kumo;
using namespace kumo::pollux;

class UnorderedStreamReaderTest : public testing::Test,
                                  public pollux::VectorBuilder {
 protected:
  void SetUp() override {
    rowType_ = ROW(
        {"c0", "c1", "c2", "c3"}, {BIGINT(), VARCHAR(), INTEGER(), BIGINT()});
  }

  class MockBatchStream : public BatchStream {
   public:
    MockBatchStream(std::vector<RowVectorPtr> rowVectors)
        : rowVectors_(std::move(rowVectors)) {}

    bool nextBatch(RowVectorPtr& batch) final {
      if (currentVector_ >= rowVectors_.size()) {
        return false;
      }
      batch = std::move(rowVectors_[currentVector_++]);
      return true;
    }

   private:
    int currentVector_{0};
    std::vector<RowVectorPtr> rowVectors_;
  };

  std::vector<RowVectorPtr> makeVectors(vector_size_t size, int numVectors) {
    std::vector<RowVectorPtr> vectors;
    VectorFuzzer::Options options;
    options.vectorSize = size;
    VectorFuzzer fuzzer(options, pool_.get(), 0);
    for (int32_t i = 0; i < numVectors; ++i) {
      auto vector =
          std::dynamic_pointer_cast<RowVector>(fuzzer.fuzzRow(rowType_));
      vectors.push_back(vector);
    }
    return vectors;
  }

  void runTest(int numStreams) {
    melon::Random::DefaultGenerator rng;
    rng.seed(numStreams);
    std::vector<std::unique_ptr<MockBatchStream>> batchStreams;
    batchStreams.reserve(numStreams);
    std::vector<RowVectorPtr> sourceVectors;
    for (int i = 0; i < numStreams; ++i) {
      auto vectors = makeVectors(
          std::max<vector_size_t>(1, melon::Random::rand32(1024, rng)),
          std::max<int>(1, melon::Random::rand32(32, rng)));
      std::copy(
          vectors.begin(), vectors.end(), std::back_inserter(sourceVectors));
      batchStreams.push_back(
          std::make_unique<MockBatchStream>(std::move(vectors)));
    }
    UnorderedStreamReader reader(std::move(batchStreams));
    RowVectorPtr readBatch;
    for (int i = 0; i < sourceVectors.size(); ++i) {
      ASSERT_TRUE(reader.nextBatch(readBatch));
      for (int j = 0; j < sourceVectors[i]->size(); ++j) {
        ASSERT_EQ(
            sourceVectors[i]->compare(readBatch.get(), j, j, CompareFlags{}),
            0);
      }
    }
    ASSERT_FALSE(reader.nextBatch(readBatch));
    ASSERT_FALSE(reader.nextBatch(readBatch));
  }

  RowTypePtr rowType_;
};

TEST_F(UnorderedStreamReaderTest, readBatch) {
  runTest(0);
  runTest(1);
  runTest(128);
}
